package com.company.笔试.常用排序算法.冒泡排序;

import com.company.笔试.常用排序算法.array.ArrayData;

import java.util.Arrays;

/**
 * @ClassName Solution
 * @Description TODO
 * @Author ylr
 * @Date 2021/7/20 18:06
 * @Version 1.0
 */
public class Solution {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(bubbleSort1(new ArrayData().getA())));
    }
    public static int[] bubbleSort(int[] arr){

        int length = arr.length;
        boolean flag =false;
        for (int i = length-1; i >0 ; i--) {
            for (int j = 0; j <i ; j++) {
                    if (arr[j]>arr[j+1]){
                        int t=arr[j];
                        arr[j]=arr[j+1];
                        arr[j+1]=t;
                        flag=true;
                    }
            }
            if (!flag){
                break;
            }
        }
        return arr;

    }
    public static int[] bubbleSort1(int[] arr){
        int flag=0;
//        {22,41,7,33,12,60,57,43,17};
        for (int i = arr.length-1; i >0; i--) {//i之后是有序的
            for (int j = 0; j <i; j++) {//j到i去比较
                if (arr[j]>arr[j+1]){
                    arr[j]=arr[j]^arr[j+1];
                    arr[i]=arr[j]^arr[j+1];
                    arr[j]=arr[j]^arr[j+1];
                    flag=1;
                }
            }
            if (flag==0) break;
        }
        return arr;
    }
}
